package com.app.xinfadi.dao;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.app.xinfadi.base.Constant;
import com.app.xinfadi.base.MyApplication;
import com.app.xinfadi.dao_bean.CenterPurchasePlanListBean;
import com.app.xinfadi.dao_bean.MartsiteJsonBean;
import com.app.xinfadi.dao_bean.PlantingSiteJsonBean;
import com.app.xinfadi.dao_bean.ProductBeanJsonBean;
import com.app.xinfadi.dao_bean.bean.CountBean;
import com.app.xinfadi.util.LogUtil;
import com.app.xinfadi.util.SharedPreferencesUtil;
import com.app.xinfadi.util.Toast;
import com.app.xinfadi.widget.gson.GsonRequest;
import com.app.xinfadi.widget.gson.Success;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * function:
 * Created by Wutengfei on 2016/1/25.
 */
public class HomeDao {
    private final static String TAG = HomeDao.class.getSimpleName();

    public HomeDao() {

    }

    //    public void getHomeInfo(final String user_id, Response.Listener<CenterPurchasePlanListBean> listener, Response.ErrorListener errorListener) {
//        String URL = Constant.URL + "login";
//        GsonRequest<CenterPurchasePlanListBean> gsonRequest = new GsonRequest<CenterPurchasePlanListBean>(Request.Method.POST, URL,
//                CenterPurchasePlanListBean.class, listener, errorListener) {
//
//            @Override
//            protected Map<String, String> getParams() throws AuthFailureError {
//                Map<String, String> params = new HashMap<String, String>();
//                params.put("user_id", SharedPreferencesUtil.getInstance().getUserLoginName());
//                return params;
//            }
//        };
//        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
//    }









    /*获取采购计划*/
    public void getPlans(final int currentPage, Response.Listener<CenterPurchasePlanListBean> listener) {
        String URL = Constant.URL + "planService/getPlans";

        GsonRequest<CenterPurchasePlanListBean> gsonRequest = new GsonRequest<CenterPurchasePlanListBean>(Request.Method.POST, URL,
                CenterPurchasePlanListBean.class, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("user_id", SharedPreferencesUtil.getInstance().getUser_Id());
                params.put("user_role", SharedPreferencesUtil.getInstance().getUserRole());
                params.put("currentPage", currentPage + "");
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }

    /*获取转增计划*/
    public void getPurchasePlans(final int currentPage, final String status, final String type, Response.Listener<CenterPurchasePlanListBean> listener) {
        String URL = Constant.URL + "planService/getPurchasePlans";
        GsonRequest<CenterPurchasePlanListBean> gsonRequest = new GsonRequest<CenterPurchasePlanListBean>(Request.Method.POST, URL,
                CenterPurchasePlanListBean.class, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("user_id", SharedPreferencesUtil.getInstance().getUser_Id());
                params.put("user_role", SharedPreferencesUtil.getInstance().getUserRole());
                params.put("currentPage", currentPage + "");
                params.put("status", status);
                params.put("type", type);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest);
    }

    /*获取中心新增计划*/
    public void getCenterPlans(final int currentPage, final String status, final String type, Response.Listener<CenterPurchasePlanListBean> listener) {
        String URL = Constant.URL + "planService/getCenterPlans";
        GsonRequest<CenterPurchasePlanListBean> gsonRequest = new GsonRequest<CenterPurchasePlanListBean>(Request.Method.POST, URL,
                CenterPurchasePlanListBean.class, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("user_id", SharedPreferencesUtil.getInstance().getUser_Id());
                params.put("user_role", SharedPreferencesUtil.getInstance().getUserRole());
                params.put("currentPage", currentPage + "");
                params.put("status", status);
                params.put("type", type);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest);
    }

    /*获取领导审查计划*/
    public void getAdminPlans(final int currentPage, final String status, final String type, Response.Listener<CenterPurchasePlanListBean> listener) {
        String URL = Constant.URL + "planService/getAdminPlans";
        GsonRequest<CenterPurchasePlanListBean> gsonRequest = new GsonRequest<CenterPurchasePlanListBean>(Request.Method.POST, URL,
                CenterPurchasePlanListBean.class, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("user_id", SharedPreferencesUtil.getInstance().getUser_Id());
                params.put("user_role", SharedPreferencesUtil.getInstance().getUserRole());
                params.put("currentPage", currentPage + "");
                params.put("status", status);
                params.put("type", type);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest);
    }





    /**
     * 获取未完成条数
     *
     * @param listener
     */
    public void getCount(Response.Listener<CountBean> listener) {
        String URL = Constant.URL + "baseService/getCount";
        GsonRequest<CountBean> gsonRequest = new GsonRequest<CountBean>(Request.Method.POST, URL,
                CountBean.class, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(MyApplication.getInstance().getApplicationContext(),"获取未执行采购计划数量失败！",Toast.LENGTH_LONG).show();
            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("user_id", SharedPreferencesUtil.getInstance().getUser_Id());
                params.put("user_role", SharedPreferencesUtil.getInstance().getUserRole());
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }

    /**
     * 获取已完成条数
     *
     * @param listener
     */
    public void getCounted(Response.Listener<CountBean> listener) {
        String URL = Constant.URL + "baseService/getCounted";
        GsonRequest<CountBean> gsonRequest = new GsonRequest<CountBean>(Request.Method.POST, URL,
                CountBean.class, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("user_id", SharedPreferencesUtil.getInstance().getUser_Id());
                params.put("user_role", SharedPreferencesUtil.getInstance().getUserRole());
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }

    /**
     * 登出报价单
     */
    public void finishPlans(File files[], String plan_id, String result, String currentStatus, String result_count, AsyncHttpResponseHandler responseHandler) throws FileNotFoundException {
        String URL = Constant.URL + "planService/finishPlans";
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(18000);
        RequestParams requestParams = new RequestParams();
        requestParams.put("result_pic", files);
        requestParams.put("plan_id", plan_id);
        requestParams.put("result", result);
        requestParams.put("currentStatus", currentStatus);
        requestParams.put("result_count", result_count);
        client.post(URL, requestParams, responseHandler);
    }

    /**
     * 登出 审核 接口
     *
     * @param status
     * @param plan_id
     * @param result
     * @param listener
     */
    public void updatePlans(final String status, final String plan_id, final String result, Response.Listener<Success> listener) {
        updatePlans(status, plan_id, result, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }

    /**
     *
     * @param status 要变成的状态
     * @param plan_id
     * @param result
     * @param listener
     * @param error
     */
    public void updatePlans(final String status, final String plan_id, final String result, Response.Listener<Success> listener, Response.ErrorListener error) {
        String URL = Constant.URL + "planService/updatePlans";
        GsonRequest<Success> gsonRequest = new GsonRequest<Success>(Request.Method.POST, URL,
                Success.class, listener, error) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("status", status);
                params.put("plan_id", plan_id);
                params.put("result", result);
                params.put("user_id", SharedPreferencesUtil.getInstance().getUser_Id());
                params.put("user_role", SharedPreferencesUtil.getInstance().getUserRole());
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }

    public void getOriginPlans(final String plan_id, Response.Listener<CenterPurchasePlanListBean> listener, Response.ErrorListener error) {
        String URL = Constant.URL + "planService/getOriginPlans";
        GsonRequest<CenterPurchasePlanListBean> gsonRequest = new GsonRequest<CenterPurchasePlanListBean>(Request.Method.POST, URL,
                CenterPurchasePlanListBean.class, listener, error) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("user_id", SharedPreferencesUtil.getInstance().getUser_Id());
                params.put("user_role", SharedPreferencesUtil.getInstance().getUserRole());
                params.put("plan_id", plan_id);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(gsonRequest, TAG);
    }
}
